/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import Clases.AlquilerSalones;
import com.google.gson.FieldNamingPolicy;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.JsonParser;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;



/**
 *
 * @author Daniel
 */
@WebServlet(name = "ServletCambioMes", urlPatterns = {"/ServletCambioMes"})
public class ServletCambioMes extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException  {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            int mes = 0;
            String convertido1= null;           
            final String callback = request.getParameter("jsoncallback");           
            String soc = null;
            AlquilerSalones eventos = new AlquilerSalones();
            List<AlquilerSalones> listaEventos = null;
            Date fecha = null;
            String salon = null;
            String fe = request.getParameter("fecha");
            mes = Integer.parseInt(fe);
            salon = request.getParameter("salon"); 
            
            try {
                listaEventos = eventos.buscarEventos(salon, mes);              
                int i = 1;
                JSONObject jsonObj = new JSONObject();
                JSONArray ja = new JSONArray();
                for (AlquilerSalones evento : listaEventos) {
                    JSONObject jasonEvento = new JSONObject();
                    jasonEvento.put("EventId", i);
                    DateFormat fechaHora = new SimpleDateFormat("yyyy-MM-dd");
                    String convertido = fechaHora.format(evento.getFechaEvento()); 
                    if (evento.getFechaRes()!=null){
                        convertido1 =  fechaHora.format(evento.getFechaRes()); 
                    }else {convertido1 = "";}
                    jasonEvento.put("Date", convertido);
                    jasonEvento.put("Title",evento.getTipoEv() );
                    jasonEvento.put("Nombre", evento.getDenominacion());
                    jasonEvento.put("Socio", evento.getSocio());
                    jasonEvento.put("TelPar", evento.getTelPar());
                    jasonEvento.put("TelCel", evento.getTelCel());
                    jasonEvento.put("Email", evento.getEmail()); 
                    jasonEvento.put("FechaRes", convertido1);
                    jasonEvento.put("Doc", evento.getDoc()); 
                    jasonEvento.put("idAlq",evento.getIdAlquilerSalon());
                    jasonEvento.put("idSoc", evento.getIdSocio());
                    jasonEvento.put("Monto",evento.getMonto());                  
                    jasonEvento.put("CssClass",evento.getEstado() );                    
                    ja.add(jasonEvento);
                    i++;           
                }
                jsonObj.put("eventos",ja);
                Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
                String Eventos = gson.toJson(jsonObj);
                //Eventos = callback + "(" + Eventos +");";                       
                response.setContentType("application/json");
                response.getWriter().write(Eventos);
                System.out.println(Eventos);
            } finally {
            }
   } 
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

